/**
 * Created by Zoe on 2016/11/8.
 */
var socket = io();
var INIT="INIT";
var WAITING="WAITING";
var DISCARD="DISCARD";
var GAMEOVER="GAMEOVER";

//出牌类型：单，对，连对，炸
var ONE = "ONE";
var TWO = "TWO";
var TWO_2 = "TWO_2";
var THREE = "THREE";
var THREE_2 = "THREE_2";
var THREE_3 = "THREE_3";
var FOUR = "FOUR";
var STRAIGHT = "STRAIGHT";
var ERROR = "ERROR";
var KING = "KING";
var fid="";
var id="";
var pkObj = {
    data:{
        status:INIT,
        count:27,
        pokers:[]
    },
    from:id,
    to: fid,
    success:0,
    init:function (){
        if(pkObj.data.status===INIT){
            $("#login").click(doInit);
        }
        socketInit();
    }
};

$(init);

function init() {
    $("body").on("click", pkObj.init());
}
function socketInit() {
    socket.on('start poker', function (data) {
        doDiscard(data);
    });
    socket.on('receive poker',function (data,count) {
        doReceive(data,count);
    });
    socket.on('crowded',function () {
        doCrowded();
    });
    socket.on('game over',function (data) {
        if(data===1)
        {
            alert("you win！");
        }
        else if(data===2){
            alert("you lose！");
        }
        else if(data===3){
            alert("你放弃啦！");
            $(".wrapper").html("");
            $(".wrapper").append(htmlStart);
            $("#login").click(doInit);
            pkObj.data.status=INIT;
        }
        else{
            alert("对方落荒而逃，恭喜你，不战而胜！");
        }
    })
}
function doInit() {
    //获得唯一的id
    var id = new Date().getTime()+""+Math.floor(Math.random()*11+1);
    pkObj.from=id;
    var obj=pkObj;
    //发送添加用户事件
    socket.emit("add user",obj);
    $(".wrapper").html("");
    $(".wrapper").append(htmlWait);
}
function getPokerType(pokerList) {
    var type = ONE;
    var n, m;
    if(pokerList.length===1){
        m=pokerList[0];
    }
    else {
        for (var i = 0; i < pokerList.length; i++) {
            n = pokerList[i];
            m = pokerList[i + 1];
            type = typeState(type, n, m);
        }
        if(type == TWO_2 || type == THREE_2 || type == THREE_3 || (type == STRAIGHT && pokerList.length < 5)) {
            type = ERROR;
        }
    }

    var result = {
        type: type,
        val: m,
        length: pokerList.length
    };
    return result;
}
function comparePoker(pokerList,oppo_pokerList) {
    var check = false;
    console.log("oppo_pokerList");
    console.log(oppo_pokerList);
    console.log("pokerList");
    console.log(pokerList);
    var data1 = getPokerType(oppo_pokerList); //对方的牌类型
    var data2 = getPokerType(pokerList);  //我的牌类型
    console.log("oppo_pokerList");
    console.log(data1);
    console.log("pokerList");
    console.log(data2);

    //1.类型相同的情况下再比较数组第一个元素大小;
    //2.4表示炸，对方不是炸，我出炸则check为true
    //3.天王炸
    if((data1.type == data2.type && (data1.length == data2.length) && pokerList[0] > oppo_pokerList[0]) || (data1.type != FOUR && data2.type == FOUR) || (data2.type == KING)) {
        check = true;
    }
    //check为true可出牌，否则不能出牌
    return check;
}
function typeState(type, n, m) {
    switch (type) {
        //单
        case ONE:
            if(n == m) {
                type = TWO;
            } else if(n == m +1 && m == 16) {
                type = KING;
            } else if(n == m + 1){
                type = STRAIGHT;
            } else {
                type = ERROR;
            }
            break;
        //对
        case TWO:
            if(n == m) {
                type = THREE;
            } else if(n == m + 1){
                type = TWO_2;
            } else {
                type = ERROR;
            }
            break;
        case TWO_2:
            if(n == m) {
                type = TWO;
            } else {
                type = ERROR;
            }
            break;
        case THREE:
            if(n == m) {
                type = FOUR;
            } else if(n == m + 1){
                type = THREE_2;
            } else {
                type = ERROR;
            }
            break;
        case THREE_2:
            if(n == m) {
                type = THREE_3;
            } else {
                type = ERROR;
            }
            break;
        case THREE_3:
            if(n == m) {
                type = THREE;
            } else {
                type = ERROR;
            }
            break;
        case STRAIGHT:
            if(n == m + 1) {
                type = STRAIGHT;
            } else {
                type = ERROR;
            }
            break;
        default:
            break
    }
    return type;
}
